package com.smart.service.impl;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smart.entity.SiteInspection;
import com.smart.entity.SysUser;
import cn.hutool.core.util.StrUtil;
import com.smart.req.InspectionParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.smart.entity.SysInspectionRecord;
import org.springframework.stereotype.Service;
import com.smart.mapper.InspectionRecordMapper;
import com.smart.service.InspectionRecordService;
import com.smart.util.SysUserUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月9日 下午5:03:37
 * Copyright (C) 深圳工勘科技
 * @说明: 巡检记录
 **/
@Service
public class InspectionRecordServiceImpl extends ServiceImpl<InspectionRecordMapper,
		SysInspectionRecord> implements InspectionRecordService{

	/**
	 * (non-Javadoc)
	 * @see com.smart.service.InspectionRecordService#save(com.smart.req.InspectionParam)
	 * 保存巡检记录
	 **/
	@Override
	@Transactional
	public int save(InspectionParam param) {
		SysInspectionRecord record = new SysInspectionRecord();
		SysUser user = SysUserUtils.getSysUser();
		BeanUtils.copyProperties(param,record);
		if(!CollectionUtils.isEmpty(param.getImgs())){
			record.setImgs(String.join(",",param.getImgs()));
		}
		record.setUserId(user.getId());
		record.setCreateTime(new Date());
		record.setModifyTime(new Date());
		return baseMapper.insert(record);
	}

	/**
	 * (non-Javadoc)
	 * @see com.smart.service.InspectionRecordService#findAll()
	 * 保存巡检分页
	 **/
	@Override
	@Transactional(readOnly=true)
	@SuppressWarnings("unchecked")
	public IPage<SysInspectionRecord> findPage(int pageNum,int pageSize,String createTime) {
		LambdaQueryWrapper<SysInspectionRecord> wrapper=new LambdaQueryWrapper<>();
		SysUser details = SysUserUtils.getSysUser();

		if(details.getId().intValue()==1){

		}else{
			wrapper.eq(SysInspectionRecord::getUserId,details.getId());
		}
		if (StrUtil.isNotEmpty(createTime)) {
			wrapper.gt(SysInspectionRecord::getCreateTime,createTime+" 00:00:00");
			wrapper.le(SysInspectionRecord::getCreateTime,createTime+" 23:59:59");
		}
		wrapper.orderByDesc(SysInspectionRecord::getCreateTime);
		return page(new Page<SysInspectionRecord>(pageNum,pageSize),wrapper);
	}

	/**
	 * (non-Javadoc)
	 * @see: 后台端分页查询
	 **/
	@Override
	@Transactional(readOnly=true)
	public IPage<SysInspectionRecord> sysFindPage(String carNo,String carType,String createTime_start,
												  String createTime_end,Integer pageNum,Integer pageSize) {
		QueryWrapper<SysInspectionRecord> wrapper = new QueryWrapper<>();


		if(StringUtils.isNotEmpty(carNo)){
			wrapper.like(SysInspectionRecord.PROP_CAR_NO,carNo);
		}
		if(StringUtils.isNotEmpty(carType)){
			wrapper.like(SysInspectionRecord.PROP_CAR_TYPE,carType);
		}
		if(StringUtils.isNotEmpty(createTime_start)){
			wrapper.gt(SysInspectionRecord.PROP_CREATETIME,createTime_start+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(createTime_end)){
			wrapper.le(SysInspectionRecord.PROP_CREATETIME,createTime_end+" 23:59:59");
		}

		wrapper.orderByDesc(SysInspectionRecord.PROP_CREATETIME);
		return page(new Page<>(pageNum,pageSize),wrapper);
	}

}